package com.sbtr.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.sbtr.common.enums.DataTypeEnum;
import com.sbtr.exception.BDException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class DataObjectConvertUtil {

    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //调用计算前转换类型，数组结构
    public static Object convert(List<String> list, String type) {
        Object p = null;
        if (type.equals(DataTypeEnum.STRING.getCode())) {
            String[] arrays = new String[list.size()];
            arrays = list.toArray(arrays);//必须制定类型
            p = ObjectUtil.clone(arrays);//数组只能转换成单个接收
        } else if (type.equals(DataTypeEnum.INTEGER.getCode())) {
            Integer[] arrays = new Integer[list.size()];
            List<Integer> list1 = list.stream().map(Integer::parseInt).collect(Collectors.toList());
            p = ObjectUtil.clone(list1.toArray(arrays));//数组只能转换成单个接收
        } else if (type.equals(DataTypeEnum.FLOAT.getCode())) {
//            double[] arrays = new double[list.size()];
            Double[] arrays = new Double[list.size()];
            List<Double> list1 = list.stream().map(Double::parseDouble).collect(Collectors.toList());
            for (int i = 0; i < list1.size(); i++) {
                arrays[i] = list1.get(i);
            }
            p = ObjectUtil.clone(list1.toArray(arrays));//数组只能转换成单个接收
//            p = arrays;//数组只能转换成单个接收
        } else if (type.equals(DataTypeEnum.FLOAT2.getCode())) {
            double[] arrays = new double[list.size()];
            List<Double> list1 = list.stream().map(Double::parseDouble).collect(Collectors.toList());
            for (int i = 0; i < list1.size(); i++) {
                arrays[i] = list1.get(i);
            }
            p = arrays;//数组只能转换成单个接收
        } else if (type.equals(DataTypeEnum.DATE.getCode())) {
            Date[] arrays = new Date[list.size()];
            List<Date> list1 = list.stream().map(r -> {
                try {
                    return sdf.parse(r);
                } catch (ParseException e) {
                    throw new BDException("日期转换失败");
                }
            }).collect(Collectors.toList());
            p = ObjectUtil.clone(list1.toArray(arrays));//数组只能转换成单个接收
        } else if (type.equals(DataTypeEnum.DATETIME.getCode())) {
            Date[] arrays = new Date[list.size()];
            List<Date> list1 = list.stream().map(r -> {
                try {
                    return sdf2.parse(r);
                } catch (ParseException e) {
                    throw new BDException("日期转换失败");
                }
            }).collect(Collectors.toList());
            p = ObjectUtil.clone(list1.toArray(arrays));//数组只能转换成单个接收
        }
        return p;
    }

    //调用计算前转换类型，基础类型结构
    public Object convert(String object, String type) {
        Object p = null;
        if (type.equals(DataTypeEnum.STRING.getCode())) {
            p = String.valueOf(object);
        } else if (type.equals(DataTypeEnum.INTEGER.getCode())) {
            p = Integer.valueOf(object);
        } else if (type.equals(DataTypeEnum.FLOAT.getCode())) {
            p = Double.valueOf(object);
        } else if (type.equals(DataTypeEnum.DATE.getCode()) || type.equals(DataTypeEnum.DATETIME.getCode())) {
            try {
                p = sdf.parse(object);
            } catch (ParseException e) {
                throw new BDException("日期转换失败");
            }
        }
        return p;
    }
}
